<?php
/**
 * Junction Expression Grammar
 * 
 * <p>Collection of all tokens which make up the Junction Expression
 * Grammar along with static helper methods to expedite grammager parsing.
 * 
 * @author Patrick J. Mizer <patrick.mizer@gmail.com>
 * @package junction.expression
 */

class Junction_Expression_Grammar
{
	/**
	 * Binary operators as defined by Junction Expression Grammar.
	 * 
	 * @var array
	 */
	private static $_binaryOperators = array(
		'&&' => true,
		'||' => true,
		'==' => true,
		'!=' => true,
		'<=' => true,
		'>=' => true,
		'<'  => true,
		'>'  => true
	);
	
	/**
	 * Unary operators as defined by the Junction Expression Grammar.
	 * 
	 * @var array
	 */
	private static $_unaryOperators = array(
		'!'  => true
	);
	
	/**
	 * Grouping tokens as defined by the Junction Expression Grammar.
	 * 
	 * @var array
	 */
	private static $_groupingTokens = array(
		'('  => true, 
		')'  => true, 
		'\'' => true, 
		'"'  => true
	);
	
	/**
	 * Token that denotes the end of an expression.
	 * 
	 * @var string
	 */
	public static $endOfExpressionToken = '~';
	
	/**
	 * Is token a binary operator as defined by the Junction Expression Grammar?
	 * 
	 * @param string token
	 * @return boolean
	 */
	public static function isBinaryOperator($token)
	{
		return self::$_binaryOperators[$token];
	}
	
	/**
	 * Is token a unary operator as defined by the Junction Expression Grammar?
	 * 
	 * @param string token
	 * @return boolean
	 */
	public static function isUnaryOperator($token)
	{
		return self::$_unaryOperators[$token];
	}
	
	/**
	 * Is token a groupiung token as defined by the Junction Expression Grammar?
	 * 
	 * @param string token
	 * @return boolean
	 */
	public static function isGroupingToken($token)
	{
		return self::$_groupingTokens[$token];
	}

	/**
	 * Is token a literal as defined by the Junction Expression Grammar?
	 * 
	 * @param string token
	 * @return boolean
	 */	
	public static function isLiteral($token)
	{
		$allTokens = array_merge(
			self::$_binaryOperators,
			self::$_unaryOperators,
			self::$_groupingTokens
		);
		
		return !$allTokens[$token];		
	}
	
	/**
	 * Get array containing all tokens.
	 * 
	 * @return array
	 */
	public static function getAllTokens()
	{
		return array_keys(
			array_merge(
				self::$_binaryOperators,
				self::$_unaryOperators,
				self::$_groupingTokens
			)
		);
	}
}
?>
